home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / c-exp.y < prev    next >
Text File  |  1992-09-11  |  38KB  |  1,527 lines

  1. /* YACC parser for C expressions, for GDB.
  2.    Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Parse a C expression from text in a string,
  21.    and return the result as a  struct expression  pointer.
  22.    That structure contains arithmetic operations in reverse polish,
  23.    with constants represented by operations that are followed by special data.
  24.    See expression.h for the details of the format.
  25.    What is important here is that it can be built up sequentially
  26.    during the process of parsing; the lower levels of the tree always
  27.    come first in the result.  */
  28.    
  29. %{
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "defs.h"
  34. #include "param.h"
  35. #include "symtab.h"
  36. #include "frame.h"
  37. #include "expression.h"
  38. #include "parser-defs.h"
  39. #include "value.h"
  40. #include "language.h"
  41.  
  42. /* These MUST be included in any grammar file!!!! 
  43.    Please choose unique names! */
  44. #define    yyparse    c_parse
  45. #define    yylex    c_lex
  46. #define    yyerror    c_error
  47. #define    yylval    c_lval
  48. #define    yychar    c_char
  49. #define    yydebug    c_debug
  50. #define    yypact    c_pact    
  51. #define    yyr1    c_r1            
  52. #define    yyr2    c_r2            
  53. #define    yydef    c_def        
  54. #define    yychk    c_chk        
  55. #define    yypgo    c_pgo        
  56. #define    yyact    c_act        
  57. #define    yyexca    c_exca
  58. #define yyerrflag c_errflag
  59. #define yynerrs    c_nerrs
  60. #define    yyps    c_ps
  61. #define    yypv    c_pv
  62. #define    yys    c_s
  63. #define    yystate    c_state
  64. #define    yytmp    c_tmp
  65. #define    yyv    c_v
  66. #define    yyval    c_val
  67. #define    yylloc    c_lloc
  68.  
  69. /* Forward decls */
  70. void yyerror ();
  71. static int parse_number ();
  72. int yyparse ();
  73.  
  74. /* #define    YYDEBUG    1 */
  75.  
  76. %}
  77.  
  78. /* Although the yacc "value" of an expression is not used,
  79.    since the result is stored in the structure being created,
  80.    other node types do have values.  */
  81.  
  82. %union
  83.   {
  84.     LONGEST lval;
  85.     unsigned LONGEST ulval;
  86.     double dval;
  87.     struct symbol *sym;
  88.     struct type *tval;
  89.     struct stoken sval;
  90.     struct ttype tsym;
  91.     struct symtoken ssym;
  92.     int voidval;
  93.     struct block *bval;
  94.     enum exp_opcode opcode;
  95.     struct internalvar *ivar;
  96.  
  97.     struct type **tvec;
  98.     int *ivec;
  99.   }
  100.  
  101. %type <voidval> exp exp1 type_exp start variable
  102. %type <tval> type typebase
  103. %type <tvec> nonempty_typelist
  104. /* %type <bval> block */
  105.  
  106. /* Fancy type parsing.  */
  107. %type <voidval> func_mod direct_abs_decl abs_decl
  108. %type <tval> ptype
  109. %type <lval> array_mod
  110.  
  111. %token <lval> INT CHAR
  112. %token <ulval> UINT
  113. %token <dval> FLOAT
  114.  
  115. /* Both NAME and TYPENAME tokens represent symbols in the input,
  116.    and both convey their data as strings.
  117.    But a TYPENAME is a string that happens to be defined as a typedef
  118.    or builtin type name (such as int or char)
  119.    and a NAME is any other symbol.
  120.    Contexts where this distinction is not important can use the
  121.    nonterminal "name", which matches either NAME or TYPENAME.  */
  122.  
  123. %token <sval> STRING
  124. %token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
  125. %token <tsym> TYPENAME
  126. %type <sval> name
  127. %type <ssym> name_not_typename
  128. %type <tsym> typename
  129.  
  130. /* A NAME_OR_INT is a symbol which is not known in the symbol table,
  131.    but which would parse as a valid number in the current input radix.
  132.    E.g. "c" when input_radix==16.  Depending on the parse, it will be
  133.    turned into a name or into a number.  NAME_OR_UINT ditto.  */
  134.  
  135. %token <ssym> NAME_OR_INT NAME_OR_UINT
  136.  
  137. %token STRUCT UNION ENUM SIZEOF UNSIGNED COLONCOLON
  138. %token ERROR
  139.  
  140. /* Special type cases, put in to allow the parser to distinguish different
  141.    legal basetypes.  */
  142. %token SIGNED LONG SHORT INT_KEYWORD
  143.  
  144. %token <lval> LAST REGNAME
  145.  
  146. %token <ivar> VARIABLE
  147.  
  148. %token <opcode> ASSIGN_MODIFY
  149.  
  150. /* C++ */
  151. %token THIS
  152.  
  153. %left ','
  154. %left ABOVE_COMMA
  155. %right '=' ASSIGN_MODIFY
  156. %right '?'
  157. %left OR
  158. %left AND
  159. %left '|'
  160. %left '^'
  161. %left '&'
  162. %left EQUAL NOTEQUAL
  163. %left '<' '>' LEQ GEQ
  164. %left LSH RSH
  165. %left '@'
  166. %left '+' '-'
  167. %left '*' '/' '%'
  168. %right UNARY INCREMENT DECREMENT
  169. %right ARROW '.' '[' '('
  170. %token <ssym> BLOCKNAME 
  171. %type <bval> block
  172. %left COLONCOLON
  173.  
  174. %%
  175.  
  176. start   :    exp1
  177.     |    type_exp
  178.     ;
  179.  
  180. type_exp:    type
  181.             { write_exp_elt_opcode(OP_TYPE);
  182.               write_exp_elt_type($1);
  183.               write_exp_elt_opcode(OP_TYPE);}
  184.     ;
  185.  
  186. /* Expressions, including the comma operator.  */
  187. exp1    :    exp
  188.     |    exp1 ',' exp
  189.             { write_exp_elt_opcode (BINOP_COMMA); }
  190.     ;
  191.  
  192. /* Expressions, not including the comma operator.  */
  193. exp    :    '*' exp    %prec UNARY
  194.             { write_exp_elt_opcode (UNOP_IND); }
  195.  
  196. exp    :    '&' exp    %prec UNARY
  197.             { write_exp_elt_opcode (UNOP_ADDR); }
  198.  
  199. exp    :    '-' exp    %prec UNARY
  200.             { write_exp_elt_opcode (UNOP_NEG); }
  201.     ;
  202.  
  203. exp    :    '!' exp    %prec UNARY
  204.             { write_exp_elt_opcode (UNOP_ZEROP); }
  205.     ;
  206.  
  207. exp    :    '~' exp    %prec UNARY
  208.             { write_exp_elt_opcode (UNOP_LOGNOT); }
  209.     ;
  210.  
  211. exp    :    INCREMENT exp    %prec UNARY
  212.             { write_exp_elt_opcode (UNOP_PREINCREMENT); }
  213.     ;
  214.  
  215. exp    :    DECREMENT exp    %prec UNARY
  216.             { write_exp_elt_opcode (UNOP_PREDECREMENT); }
  217.     ;
  218.  
  219. exp    :    exp INCREMENT    %prec UNARY
  220.             { write_exp_elt_opcode (UNOP_POSTINCREMENT); }
  221.     ;
  222.  
  223. exp    :    exp DECREMENT    %prec UNARY
  224.             { write_exp_elt_opcode (UNOP_POSTDECREMENT); }
  225.     ;
  226.  
  227. exp    :    SIZEOF exp       %prec UNARY
  228.             { write_exp_elt_opcode (UNOP_SIZEOF); }
  229.     ;
  230.  
  231. exp    :    exp ARROW name
  232.             { write_exp_elt_opcode (STRUCTOP_PTR);
  233.               write_exp_string ($3);
  234.               write_exp_elt_opcode (STRUCTOP_PTR); }
  235.     ;
  236.  
  237. exp    :    exp ARROW '*' exp
  238.             { write_exp_elt_opcode (STRUCTOP_MPTR); }
  239.     ;
  240.  
  241. exp    :    exp '.' name
  242.             { write_exp_elt_opcode (STRUCTOP_STRUCT);
  243.               write_exp_string ($3);
  244.               write_exp_elt_opcode (STRUCTOP_STRUCT); }
  245.     ;
  246.  
  247. exp    :    exp '.' '*' exp
  248.             { write_exp_elt_opcode (STRUCTOP_MEMBER); }
  249.     ;
  250.  
  251. exp    :    exp '[' exp1 ']'
  252.             { write_exp_elt_opcode (BINOP_SUBSCRIPT); }
  253.     ;
  254.  
  255. exp    :    exp '(' 
  256.             /* This is to save the value of arglist_len
  257.                being accumulated by an outer function call.  */
  258.             { start_arglist (); }
  259.         arglist ')'    %prec ARROW
  260.             { write_exp_elt_opcode (OP_FUNCALL);
  261.               write_exp_elt_longcst ((LONGEST) end_arglist ());
  262.               write_exp_elt_opcode (OP_FUNCALL); }
  263.     ;
  264.  
  265. arglist    :
  266.     ;
  267.  
  268. arglist    :    exp
  269.             { arglist_len = 1; }
  270.     ;
  271.  
  272. arglist    :    arglist ',' exp   %prec ABOVE_COMMA
  273.             { arglist_len++; }
  274.     ;
  275.  
  276. exp    :    '{' type '}' exp  %prec UNARY
  277.             { write_exp_elt_opcode (UNOP_MEMVAL);
  278.               write_exp_elt_type ($2);
  279.               write_exp_elt_opcode (UNOP_MEMVAL); }
  280.     ;
  281.  
  282. exp    :    '(' type ')' exp  %prec UNARY
  283.             { write_exp_elt_opcode (UNOP_CAST);
  284.               write_exp_elt_type ($2);
  285.               write_exp_elt_opcode (UNOP_CAST); }
  286.     ;
  287.  
  288. exp    :    '(' exp1 ')'
  289.             { }
  290.     ;
  291.  
  292. /* Binary operators in order of decreasing precedence.  */
  293.  
  294. exp    :    exp '@' exp
  295.             { write_exp_elt_opcode (BINOP_REPEAT); }
  296.     ;
  297.  
  298. exp    :    exp '*' exp
  299.             { write_exp_elt_opcode (BINOP_MUL); }
  300.     ;
  301.  
  302. exp    :    exp '/' exp
  303.             { write_exp_elt_opcode (BINOP_DIV); }
  304.     ;
  305.  
  306. exp    :    exp '%' exp
  307.             { write_exp_elt_opcode (BINOP_REM); }
  308.     ;
  309.  
  310. exp    :    exp '+' exp
  311.             { write_exp_elt_opcode (BINOP_ADD); }
  312.     ;
  313.  
  314. exp    :    exp '-' exp
  315.             { write_exp_elt_opcode (BINOP_SUB); }
  316.     ;
  317.  
  318. exp    :    exp LSH exp
  319.             { write_exp_elt_opcode (BINOP_LSH); }
  320.     ;
  321.  
  322. exp    :    exp RSH exp
  323.             { write_exp_elt_opcode (BINOP_RSH); }
  324.     ;
  325.  
  326. exp    :    exp EQUAL exp
  327.             { write_exp_elt_opcode (BINOP_EQUAL); }
  328.     ;
  329.  
  330. exp    :    exp NOTEQUAL exp
  331.             { write_exp_elt_opcode (BINOP_NOTEQUAL); }
  332.     ;
  333.  
  334. exp    :    exp LEQ exp
  335.             { write_exp_elt_opcode (BINOP_LEQ); }
  336.     ;
  337.  
  338. exp    :    exp GEQ exp
  339.             { write_exp_elt_opcode (BINOP_GEQ); }
  340.     ;
  341.  
  342. exp    :    exp '<' exp
  343.             { write_exp_elt_opcode (BINOP_LESS); }
  344.     ;
  345.  
  346. exp    :    exp '>' exp
  347.             { write_exp_elt_opcode (BINOP_GTR); }
  348.     ;
  349.  
  350. exp    :    exp '&' exp
  351.             { write_exp_elt_opcode (BINOP_LOGAND); }
  352.     ;
  353.  
  354. exp    :    exp '^' exp
  355.             { write_exp_elt_opcode (BINOP_LOGXOR); }
  356.     ;
  357.  
  358. exp    :    exp '|' exp
  359.             { write_exp_elt_opcode (BINOP_LOGIOR); }
  360.     ;
  361.  
  362. exp    :    exp AND exp
  363.             { write_exp_elt_opcode (BINOP_AND); }
  364.     ;
  365.  
  366. exp    :    exp OR exp
  367.             { write_exp_elt_opcode (BINOP_OR); }
  368.     ;
  369.  
  370. exp    :    exp '?' exp ':' exp    %prec '?'
  371.             { write_exp_elt_opcode (TERNOP_COND); }
  372.     ;
  373.               
  374. exp    :    exp '=' exp
  375.             { write_exp_elt_opcode (BINOP_ASSIGN); }
  376.     ;
  377.  
  378. exp    :    exp ASSIGN_MODIFY exp
  379.             { write_exp_elt_opcode (BINOP_ASSIGN_MODIFY);
  380.               write_exp_elt_opcode ($2);
  381.               write_exp_elt_opcode (BINOP_ASSIGN_MODIFY); }
  382.     ;
  383.  
  384. exp    :    INT
  385.             { write_exp_elt_opcode (OP_LONG);
  386.               if ($1 == (int) $1 || $1 == (unsigned int) $1)
  387.                 write_exp_elt_type (builtin_type_int);
  388.               else
  389.                 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
  390.               write_exp_elt_longcst ((LONGEST) $1);
  391.               write_exp_elt_opcode (OP_LONG); }
  392.     ;
  393.  
  394. exp    :    NAME_OR_INT
  395.             { YYSTYPE val;
  396.               parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
  397.               write_exp_elt_opcode (OP_LONG);
  398.               if (val.lval == (int) val.lval ||
  399.                   val.lval == (unsigned int) val.lval)
  400.                 write_exp_elt_type (builtin_type_int);
  401.               else
  402.                 write_exp_elt_type (BUILTIN_TYPE_LONGEST);
  403.               write_exp_elt_longcst (val.lval);
  404.               write_exp_elt_opcode (OP_LONG); }
  405.     ;
  406.  
  407. exp    :    UINT
  408.             {
  409.               write_exp_elt_opcode (OP_LONG);
  410.               if ($1 == (unsigned int) $1)
  411.                 write_exp_elt_type (builtin_type_unsigned_int);
  412.               else
  413.                 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
  414.               write_exp_elt_longcst ((LONGEST) $1);
  415.               write_exp_elt_opcode (OP_LONG);
  416.             }
  417.     ;
  418.  
  419. exp    :    NAME_OR_UINT
  420.             { YYSTYPE val;
  421.               parse_number ($1.stoken.ptr, $1.stoken.length, 0, &val);
  422.               write_exp_elt_opcode (OP_LONG);
  423.               if (val.ulval == (unsigned int) val.ulval)
  424.                 write_exp_elt_type (builtin_type_unsigned_int);
  425.               else
  426.                 write_exp_elt_type (BUILTIN_TYPE_UNSIGNED_LONGEST);
  427.               write_exp_elt_longcst ((LONGEST)val.ulval);
  428.               write_exp_elt_opcode (OP_LONG);
  429.             }
  430.     ;
  431.  
  432. exp    :    CHAR
  433.             { write_exp_elt_opcode (OP_LONG);
  434.               write_exp_elt_type (builtin_type_char);
  435.               write_exp_elt_longcst ((LONGEST) $1);
  436.               write_exp_elt_opcode (OP_LONG); }
  437.     ;
  438.  
  439. exp    :    FLOAT
  440.             { write_exp_elt_opcode (OP_DOUBLE);
  441.               write_exp_elt_type (builtin_type_double);
  442.               write_exp_elt_dblcst ($1);
  443.               write_exp_elt_opcode (OP_DOUBLE); }
  444.     ;
  445.  
  446. exp    :    variable
  447.     ;
  448.  
  449. exp    :    LAST
  450.             { write_exp_elt_opcode (OP_LAST);
  451.               write_exp_elt_longcst ((LONGEST) $1);
  452.               write_exp_elt_opcode (OP_LAST); }
  453.     ;
  454.  
  455. exp    :    REGNAME
  456.             { write_exp_elt_opcode (OP_REGISTER);
  457.               write_exp_elt_longcst ((LONGEST) $1);
  458.               write_exp_elt_opcode (OP_REGISTER); }
  459.     ;
  460.  
  461. exp    :    VARIABLE
  462.             { write_exp_elt_opcode (OP_INTERNALVAR);
  463.               write_exp_elt_intern ($1);
  464.               write_exp_elt_opcode (OP_INTERNALVAR); }
  465.     ;
  466.  
  467. exp    :    SIZEOF '(' type ')'    %prec UNARY
  468.             { write_exp_elt_opcode (OP_LONG);
  469.               write_exp_elt_type (builtin_type_int);
  470.               write_exp_elt_longcst ((LONGEST) TYPE_LENGTH ($3));
  471.               write_exp_elt_opcode (OP_LONG); }
  472.     ;
  473.  
  474. exp    :    STRING
  475.             { write_exp_elt_opcode (OP_STRING);
  476.               write_exp_string ($1);
  477.               write_exp_elt_opcode (OP_STRING); }
  478.     ;
  479.  
  480. /* C++.  */
  481. exp    :    THIS
  482.             { write_exp_elt_opcode (OP_THIS);
  483.               write_exp_elt_opcode (OP_THIS); }
  484.     ;
  485.  
  486. /* end of C++.  */
  487.  
  488. block    :    BLOCKNAME
  489.             {
  490.               if ($1.sym != 0)
  491.                   $$ = SYMBOL_BLOCK_VALUE ($1.sym);
  492.               else
  493.                 {
  494.                   struct symtab *tem =
  495.                   lookup_symtab (copy_name ($1.stoken));
  496.                   if (tem)
  497.                 $$ = BLOCKVECTOR_BLOCK
  498.                      (BLOCKVECTOR (tem), STATIC_BLOCK);
  499.                   else
  500.                 error ("No file or function \"%s\".",
  501.                        copy_name ($1.stoken));
  502.                 }
  503.             }
  504.     ;
  505.  
  506. block    :    block COLONCOLON name
  507.             { struct symbol *tem
  508.                 = lookup_symbol (copy_name ($3), $1,
  509.                          VAR_NAMESPACE, 0, NULL);
  510.               if (!tem || SYMBOL_CLASS (tem) != LOC_BLOCK)
  511.                 error ("No function \"%s\" in specified context.",
  512.                    copy_name ($3));
  513.               $$ = SYMBOL_BLOCK_VALUE (tem); }
  514.     ;
  515.  
  516. variable:    block COLONCOLON name
  517.             { struct symbol *sym;
  518.               sym = lookup_symbol (copy_name ($3), $1,
  519.                            VAR_NAMESPACE, 0, NULL);
  520.               if (sym == 0)
  521.                 error ("No symbol \"%s\" in specified context.",
  522.                    copy_name ($3));
  523.  
  524.               write_exp_elt_opcode (OP_VAR_VALUE);
  525.               write_exp_elt_sym (sym);
  526.               write_exp_elt_opcode (OP_VAR_VALUE); }
  527.     ;
  528.  
  529. variable:    typebase COLONCOLON name
  530.             {
  531.               struct type *type = $1;
  532.               if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  533.                   && TYPE_CODE (type) != TYPE_CODE_UNION)
  534.                 error ("`%s' is not defined as an aggregate type.",
  535.                    TYPE_NAME (type));
  536.  
  537.               write_exp_elt_opcode (OP_SCOPE);
  538.               write_exp_elt_type (type);
  539.               write_exp_string ($3);
  540.               write_exp_elt_opcode (OP_SCOPE);
  541.             }
  542.     |    typebase COLONCOLON '~' name
  543.             {
  544.               struct type *type = $1;
  545.               if (TYPE_CODE (type) != TYPE_CODE_STRUCT
  546.                   && TYPE_CODE (type) != TYPE_CODE_UNION)
  547.                 error ("`%s' is not defined as an aggregate type.",
  548.                    TYPE_NAME (type));
  549.  
  550.               if (strcmp (type_name_no_tag (type), $4.ptr))
  551.                 error ("invalid destructor `%s::~%s'",
  552.                    type_name_no_tag (type), $4.ptr);
  553.  
  554.               write_exp_elt_opcode (OP_SCOPE);
  555.               write_exp_elt_type (type);
  556.               write_exp_string ($4);
  557.               write_exp_elt_opcode (OP_SCOPE);
  558.               write_exp_elt_opcode (UNOP_LOGNOT);
  559.             }
  560.     |    COLONCOLON name
  561.             {
  562.               char *name = copy_name ($2);
  563.               struct symbol *sym;
  564.               int i;
  565.  
  566.               sym =
  567.                 lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
  568.               if (sym)
  569.                 {
  570.                   write_exp_elt_opcode (OP_VAR_VALUE);
  571.                   write_exp_elt_sym (sym);
  572.                   write_exp_elt_opcode (OP_VAR_VALUE);
  573.                   break;
  574.                 }
  575.               for (i = 0; i < misc_function_count; i++)
  576.                 if (!strcmp (misc_function_vector[i].name, name))
  577.                   break;
  578.  
  579.               if (i < misc_function_count)
  580.                 {
  581.                   enum misc_function_type mft =
  582.                   misc_function_vector[i].type;
  583.                   
  584.                   write_exp_elt_opcode (OP_LONG);
  585.                   write_exp_elt_type (builtin_type_int);
  586.                   write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
  587.                   write_exp_elt_opcode (OP_LONG);
  588.                   write_exp_elt_opcode (UNOP_MEMVAL);
  589.                   if (mft == mf_data || mft == mf_bss)
  590.                 write_exp_elt_type (builtin_type_int);
  591.                   else if (mft == mf_text)
  592.                 write_exp_elt_type (lookup_function_type (builtin_type_int));
  593.                   else
  594.                 write_exp_elt_type (builtin_type_char);
  595.                   write_exp_elt_opcode (UNOP_MEMVAL);
  596.                 }
  597.               else
  598.                 if (symtab_list == 0
  599.                 && partial_symtab_list == 0)
  600.                   error ("No symbol table is loaded.  Use the \"file\" command.");
  601.                 else
  602.                   error ("No symbol \"%s\" in current context.", name);
  603.             }
  604.     ;
  605.  
  606. variable:    name_not_typename
  607.             { struct symbol *sym = $1.sym;
  608.  
  609.               if (sym)
  610.                 {
  611.                   switch (SYMBOL_CLASS (sym))
  612.                 {
  613.                 case LOC_REGISTER:
  614.                 case LOC_ARG:
  615.                 case LOC_REF_ARG:
  616.                 case LOC_REGPARM:
  617.                 case LOC_LOCAL:
  618.                 case LOC_LOCAL_ARG:
  619.                   if (innermost_block == 0 ||
  620.                       contained_in (block_found, 
  621.                             innermost_block))
  622.                     innermost_block = block_found;
  623.                 case LOC_UNDEF:
  624.                 case LOC_CONST:
  625.                 case LOC_STATIC:
  626.                 case LOC_TYPEDEF:
  627.                 case LOC_LABEL:
  628.                 case LOC_BLOCK:
  629.                 case LOC_CONST_BYTES:
  630.  
  631.                   /* In this case the expression can
  632.                      be evaluated regardless of what
  633.                      frame we are in, so there is no
  634.                      need to check for the
  635.                      innermost_block.  These cases are
  636.                      listed so that gcc -Wall will
  637.                      report types that may not have
  638.                      been considered.  */
  639.  
  640.                   break;
  641.                 }
  642.                   write_exp_elt_opcode (OP_VAR_VALUE);
  643.                   write_exp_elt_sym (sym);
  644.                   write_exp_elt_opcode (OP_VAR_VALUE);
  645.                 }
  646.               else if ($1.is_a_field_of_this)
  647.                 {
  648.                   /* C++: it hangs off of `this'.  Must
  649.                      not inadvertently convert from a method call
  650.                  to data ref.  */
  651.                   if (innermost_block == 0 || 
  652.                   contained_in (block_found, innermost_block))
  653.                 innermost_block = block_found;
  654.                   write_exp_elt_opcode (OP_THIS);
  655.                   write_exp_elt_opcode (OP_THIS);
  656.                   write_exp_elt_opcode (STRUCTOP_PTR);
  657.                   write_exp_string ($1.stoken);
  658.                   write_exp_elt_opcode (STRUCTOP_PTR);
  659.                 }
  660.               else
  661.                 {
  662.                   register int i;
  663.                   register char *arg = copy_name ($1.stoken);
  664.  
  665.                 /* FIXME, this search is linear!  At least
  666.                    optimize the strcmp with a 1-char cmp... */
  667.                   for (i = 0; i < misc_function_count; i++)
  668.                 if (!strcmp (misc_function_vector[i].name, arg))
  669.                   break;
  670.  
  671.                   if (i < misc_function_count)
  672.                 {
  673.                   enum misc_function_type mft =
  674.                       misc_function_vector[i].type;
  675.                   
  676.                   write_exp_elt_opcode (OP_LONG);
  677.                   write_exp_elt_type (builtin_type_int);
  678.                   write_exp_elt_longcst ((LONGEST) misc_function_vector[i].address);
  679.                   write_exp_elt_opcode (OP_LONG);
  680.                   write_exp_elt_opcode (UNOP_MEMVAL);
  681.                   if (mft == mf_data || mft == mf_bss)
  682.                     write_exp_elt_type (builtin_type_int);
  683.                   else if (mft == mf_text)
  684.                     write_exp_elt_type (lookup_function_type (builtin_type_int));
  685.                   else
  686.                     write_exp_elt_type (builtin_type_char);
  687.                   write_exp_elt_opcode (UNOP_MEMVAL);
  688.                 }
  689.                   else if (symtab_list == 0
  690.                        && partial_symtab_list == 0)
  691.                 error ("No symbol table is loaded.  Use the \"file\" command.");
  692.                   else
  693.                 error ("No symbol \"%s\" in current context.",
  694.                        copy_name ($1.stoken));
  695.                 }
  696.             }
  697.     ;
  698.  
  699.  
  700. ptype    :    typebase
  701.     |    typebase abs_decl
  702.         {
  703.           /* This is where the interesting stuff happens.  */
  704.           int done = 0;
  705.           int array_size;
  706.           struct type *follow_type = $1;
  707.           
  708.           while (!done)
  709.             switch (pop_type ())
  710.               {
  711.               case tp_end:
  712.             done = 1;
  713.             break;
  714.               case tp_pointer:
  715.             follow_type = lookup_pointer_type (follow_type);
  716.             break;
  717.               case tp_reference:
  718.             follow_type = lookup_reference_type (follow_type);
  719.             break;
  720.               case tp_array:
  721.             array_size = pop_type_int ();
  722.             if (array_size != -1)
  723.               follow_type = create_array_type (follow_type,
  724.                                array_size);
  725.             else
  726.               follow_type = lookup_pointer_type (follow_type);
  727.             break;
  728.               case tp_function:
  729.             follow_type = lookup_function_type (follow_type);
  730.             break;
  731.               }
  732.           $$ = follow_type;
  733.         }
  734.     ;
  735.  
  736. abs_decl:    '*'
  737.             { push_type (tp_pointer); $$ = 0; }
  738.     |    '*' abs_decl
  739.             { push_type (tp_pointer); $$ = $2; }
  740.     |    '&'
  741.             { push_type (tp_reference); $$ = 0; }
  742.     |    '&' abs_decl
  743.             { push_type (tp_reference); $$ = $2; }
  744.     |    direct_abs_decl
  745.     ;
  746.  
  747. direct_abs_decl: '(' abs_decl ')'
  748.             { $$ = $2; }
  749.     |    direct_abs_decl array_mod
  750.             {
  751.               push_type_int ($2);
  752.               push_type (tp_array);
  753.             }
  754.     |    array_mod
  755.             {
  756.               push_type_int ($1);
  757.               push_type (tp_array);
  758.               $$ = 0;
  759.             }
  760.     |     direct_abs_decl func_mod
  761.             { push_type (tp_function); }
  762.     |    func_mod
  763.             { push_type (tp_function); }
  764.     ;
  765.  
  766. array_mod:    '[' ']'
  767.             { $$ = -1; }
  768.     |    '[' INT ']'
  769.             { $$ = $2; }
  770.     ;
  771.  
  772. func_mod:    '(' ')'
  773.             { $$ = 0; }
  774.     ;
  775.  
  776. type    :    ptype
  777.     |    typebase COLONCOLON '*'
  778.             { $$ = lookup_member_type (builtin_type_int, $1); }
  779.     |    type '(' typebase COLONCOLON '*' ')'
  780.             { $$ = lookup_member_type ($1, $3); }
  781.     |    type '(' typebase COLONCOLON '*' ')' '(' ')'
  782.             { $$ = lookup_member_type
  783.                 (lookup_function_type ($1), $3); }
  784.     |    type '(' typebase COLONCOLON '*' ')' '(' nonempty_typelist ')'
  785.             { $$ = lookup_member_type
  786.                 (lookup_function_type ($1), $3);
  787.               free ($8); }
  788.     ;
  789.  
  790. typebase
  791.     :    TYPENAME
  792.             { $$ = $1.type; }
  793.     |    INT_KEYWORD
  794.             { $$ = builtin_type_int; }
  795.     |    LONG
  796.             { $$ = builtin_type_long; }
  797.     |    SHORT
  798.             { $$ = builtin_type_short; }
  799.     |    LONG INT_KEYWORD
  800.             { $$ = builtin_type_long; }
  801.     |    UNSIGNED LONG INT_KEYWORD
  802.             { $$ = builtin_type_unsigned_long; }
  803.     |    LONG LONG
  804.             { $$ = builtin_type_long_long; }
  805.     |    LONG LONG INT_KEYWORD
  806.             { $$ = builtin_type_long_long; }
  807.     |    UNSIGNED LONG LONG
  808.             { $$ = builtin_type_unsigned_long_long; }
  809.     |    UNSIGNED LONG LONG INT_KEYWORD
  810.             { $$ = builtin_type_unsigned_long_long; }
  811.     |    SHORT INT_KEYWORD
  812.             { $$ = builtin_type_short; }
  813.     |    UNSIGNED SHORT INT_KEYWORD
  814.             { $$ = builtin_type_unsigned_short; }
  815.     |    STRUCT name
  816.             { $$ = lookup_struct (copy_name ($2),
  817.                           expression_context_block); }
  818.     |    UNION name
  819.             { $$ = lookup_union (copy_name ($2),
  820.                          expression_context_block); }
  821.     |    ENUM name
  822.             { $$ = lookup_enum (copy_name ($2),
  823.                         expression_context_block); }
  824.     |    UNSIGNED typename
  825.             { $$ = lookup_unsigned_typename (TYPE_NAME($2.type)); }
  826.     |    UNSIGNED
  827.             { $$ = builtin_type_unsigned_int; }
  828.     |    SIGNED typename
  829.             { $$ = $2.type; }
  830.     |    SIGNED
  831.             { $$ = builtin_type_int; }
  832.     ;
  833.  
  834. typename:    TYPENAME
  835.     |    INT_KEYWORD
  836.         {
  837.           $$.stoken.ptr = "int";
  838.           $$.stoken.length = 3;
  839.           $$.type = builtin_type_int;
  840.         }
  841.     |    LONG
  842.         {
  843.           $$.stoken.ptr = "long";
  844.           $$.stoken.length = 4;
  845.           $$.type = builtin_type_long;
  846.         }
  847.     |    SHORT
  848.         {
  849.           $$.stoken.ptr = "short";
  850.           $$.stoken.length = 5;
  851.           $$.type = builtin_type_short;
  852.         }
  853.     ;
  854.  
  855. nonempty_typelist
  856.     :    type
  857.         { $$ = (struct type **)xmalloc (sizeof (struct type *) * 2);
  858.           $$[0] = (struct type *)0;
  859.           $$[1] = $1;
  860.         }
  861.     |    nonempty_typelist ',' type
  862.         { int len = sizeof (struct type *) * ++($<ivec>1[0]);
  863.           $$ = (struct type **)xrealloc ($1, len);
  864.           $$[$<ivec>$[0]] = $3;
  865.         }
  866.     ;
  867.  
  868. name    :    NAME { $$ = $1.stoken; }
  869.     |    BLOCKNAME { $$ = $1.stoken; }
  870.     |    TYPENAME { $$ = $1.stoken; }
  871.     |    NAME_OR_INT  { $$ = $1.stoken; }
  872.     |    NAME_OR_UINT  { $$ = $1.stoken; }
  873.     ;
  874.  
  875. name_not_typename :    NAME
  876.     |    BLOCKNAME
  877. /* These would be useful if name_not_typename was useful, but it is just
  878.    a fake for "variable", so these cause reduce/reduce conflicts because
  879.    the parser can't tell whether NAME_OR_INT is a name_not_typename (=variable,
  880.    =exp) or just an exp.  If name_not_typename was ever used in an lvalue
  881.    context where only a name could occur, this might be useful.
  882.       |    NAME_OR_INT
  883.       |    NAME_OR_UINT
  884.  */
  885.     ;
  886.  
  887. %%
  888.  
  889. /* Take care of parsing a number (anything that starts with a digit).
  890.    Set yylval and return the token type; update lexptr.
  891.    LEN is the number of characters in it.  */
  892.  
  893. /*** Needs some error checking for the float case ***/
  894.  
  895. static int
  896. parse_number (p, len, parsed_float, putithere)
  897.      register char *p;
  898.      register int len;
  899.      int parsed_float;
  900.      YYSTYPE *putithere;
  901. {
  902.   register LONGEST n = 0;
  903.   register LONGEST prevn = 0;
  904.   register int i;
  905.   register int c;
  906.   register int base = input_radix;
  907.   int unsigned_p = 0;
  908.  
  909.   extern double atof ();
  910.  
  911.   if (parsed_float)
  912.     {
  913.       /* It's a float since it contains a point or an exponent.  */
  914.       putithere->dval = atof (p);
  915.       return FLOAT;
  916.     }
  917.  
  918.   /* Handle base-switching prefixes 0x, 0t, 0d, 0 */
  919.   if (p[0] == '0')
  920.     switch (p[1])
  921.       {
  922.       case 'x':
  923.       case 'X':
  924.     if (len >= 3)
  925.       {
  926.         p += 2;
  927.         base = 16;
  928.         len -= 2;
  929.       }
  930.     break;
  931.  
  932.       case 't':
  933.       case 'T':
  934.       case 'd':
  935.       case 'D':
  936.     if (len >= 3)
  937.       {
  938.         p += 2;
  939.         base = 10;
  940.         len -= 2;
  941.       }
  942.     break;
  943.  
  944.       default:
  945.     base = 8;
  946.     break;
  947.       }
  948.  
  949.   while (len-- > 0)
  950.     {
  951.       c = *p++;
  952.       if (c >= 'A' && c <= 'Z')
  953.     c += 'a' - 'A';
  954.       if (c != 'l' && c != 'u')
  955.     n *= base;
  956.       if (c >= '0' && c <= '9')
  957.     n += i = c - '0';
  958.       else
  959.     {
  960.       if (base > 10 && c >= 'a' && c <= 'f')
  961.         n += i = c - 'a' + 10;
  962.       else if (len == 0 && c == 'l')
  963.         ;
  964.       else if (len == 0 && c == 'u')
  965.         unsigned_p = 1;
  966.       else
  967.         return ERROR;    /* Char not a digit */
  968.     }
  969.       if (i >= base)
  970.     return ERROR;        /* Invalid digit in this base */
  971.       if(!unsigned_p && (prevn >= n))
  972.      unsigned_p=1;        /* Try something unsigned */
  973.       /* Don't do the range check if n==i and i==0, since that special
  974.      case will give an overflow error. */
  975.       if(RANGE_CHECK && n!=0)
  976.       {    
  977.      if((unsigned_p && (unsigned)prevn >= (unsigned)n))
  978.         range_error("Overflow on numeric constant.");     
  979.       }
  980.       prevn=n;
  981.     }
  982.  
  983.   if (unsigned_p)
  984.     {
  985.       putithere->ulval = n;
  986.       return UINT;
  987.     }
  988.   else
  989.     {
  990.       putithere->lval = n;
  991.       return INT;
  992.     }
  993. }
  994.  
  995. struct token
  996. {
  997.   char *operator;
  998.   int token;
  999.   enum exp_opcode opcode;
  1000. };
  1001.  
  1002. const static struct token tokentab3[] =
  1003.   {
  1004.     {">>=", ASSIGN_MODIFY, BINOP_RSH},
  1005.     {"<<=", ASSIGN_MODIFY, BINOP_LSH}
  1006.   };
  1007.  
  1008. const static struct token tokentab2[] =
  1009.   {
  1010.     {"+=", ASSIGN_MODIFY, BINOP_ADD},
  1011.     {"-=", ASSIGN_MODIFY, BINOP_SUB},
  1012.     {"*=", ASSIGN_MODIFY, BINOP_MUL},
  1013.     {"/=", ASSIGN_MODIFY, BINOP_DIV},
  1014.     {"%=", ASSIGN_MODIFY, BINOP_REM},
  1015.     {"|=", ASSIGN_MODIFY, BINOP_LOGIOR},
  1016.     {"&=", ASSIGN_MODIFY, BINOP_LOGAND},
  1017.     {"^=", ASSIGN_MODIFY, BINOP_LOGXOR},
  1018.     {"++", INCREMENT, BINOP_END},
  1019.     {"--", DECREMENT, BINOP_END},
  1020.     {"->", ARROW, BINOP_END},
  1021.     {"&&", AND, BINOP_END},
  1022.     {"||", OR, BINOP_END},
  1023.     {"::", COLONCOLON, BINOP_END},
  1024.     {"<<", LSH, BINOP_END},
  1025.     {">>", RSH, BINOP_END},
  1026.     {"==", EQUAL, BINOP_END},
  1027.     {"!=", NOTEQUAL, BINOP_END},
  1028.     {"<=", LEQ, BINOP_END},
  1029.     {">=", GEQ, BINOP_END}
  1030.   };
  1031.  
  1032. /* Read one token, getting characters through lexptr.  */
  1033.  
  1034. int
  1035. yylex ()
  1036. {
  1037.   register int c;
  1038.   register int namelen;
  1039.   register unsigned i;
  1040.   register char *tokstart;
  1041.  
  1042.  retry:
  1043.  
  1044.   tokstart = lexptr;
  1045.   /* See if it is a special token of length 3.  */
  1046.   for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++)
  1047.     if (!strncmp (tokstart, tokentab3[i].operator, 3))
  1048.       {
  1049.     lexptr += 3;
  1050.     yylval.opcode = tokentab3[i].opcode;
  1051.     return tokentab3[i].token;
  1052.       }
  1053.  
  1054.   /* See if it is a special token of length 2.  */
  1055.   for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++)
  1056.     if (!strncmp (tokstart, tokentab2[i].operator, 2))
  1057.       {
  1058.     lexptr += 2;
  1059.     yylval.opcode = tokentab2[i].opcode;
  1060.     return tokentab2[i].token;
  1061.       }
  1062.  
  1063.   switch (c = *tokstart)
  1064.     {
  1065.     case 0:
  1066.       return 0;
  1067.  
  1068.     case ' ':
  1069.     case '\t':
  1070.     case '\n':
  1071.       lexptr++;
  1072.       goto retry;
  1073.  
  1074.     case '\'':
  1075.       lexptr++;
  1076.       c = *lexptr++;
  1077.       if (c == '\\')
  1078.     c = parse_escape (&lexptr);
  1079.       yylval.lval = c;
  1080.       c = *lexptr++;
  1081.       if (c != '\'')
  1082.     error ("Invalid character constant.");
  1083.       return CHAR;
  1084.  
  1085.     case '(':
  1086.       paren_depth++;
  1087.       lexptr++;
  1088.       return c;
  1089.  
  1090.     case ')':
  1091.       if (paren_depth == 0)
  1092.     return 0;
  1093.       paren_depth--;
  1094.       lexptr++;
  1095.       return c;
  1096.  
  1097.     case ',':
  1098.       if (comma_terminates && paren_depth == 0)
  1099.     return 0;
  1100.       lexptr++;
  1101.       return c;
  1102.  
  1103.     case '.':
  1104.       /* Might be a floating point number.  */
  1105.       if (lexptr[1] < '0' || lexptr[1] > '9')
  1106.     goto symbol;        /* Nope, must be a symbol. */
  1107.       /* FALL THRU into number case.  */
  1108.  
  1109.     case '0':
  1110.     case '1':
  1111.     case '2':
  1112.     case '3':
  1113.     case '4':
  1114.     case '5':
  1115.     case '6':
  1116.     case '7':
  1117.     case '8':
  1118.     case '9':
  1119.       {
  1120.     /* It's a number.  */
  1121.     int got_dot = 0, got_e = 0, toktype;
  1122.     register char *p = tokstart;
  1123.     int hex = input_radix > 10;
  1124.  
  1125.     if (c == '0' && (p[1] == 'x' || p[1] == 'X'))
  1126.       {
  1127.         p += 2;
  1128.         hex = 1;
  1129.       }
  1130.     else if (c == '0' && (p[1]=='t' || p[1]=='T' || p[1]=='d' || p[1]=='D'))
  1131.       {
  1132.         p += 2;
  1133.         hex = 0;
  1134.       }
  1135.  
  1136.     for (;; ++p)
  1137.       {
  1138.         if (!hex && !got_e && (*p == 'e' || *p == 'E'))
  1139.           got_dot = got_e = 1;
  1140.         else if (!hex && !got_dot && *p == '.')
  1141.           got_dot = 1;
  1142.         else if (got_e && (p[-1] == 'e' || p[-1] == 'E')
  1143.              && (*p == '-' || *p == '+'))
  1144.           /* This is the sign of the exponent, not the end of the
  1145.          number.  */
  1146.           continue;
  1147.         /* We will take any letters or digits.  parse_number will
  1148.            complain if past the radix, or if L or U are not final.  */
  1149.         else if ((*p < '0' || *p > '9')
  1150.              && ((*p < 'a' || *p > 'z')
  1151.                   && (*p < 'A' || *p > 'Z')))
  1152.           break;
  1153.       }
  1154.     toktype = parse_number (tokstart, p - tokstart, got_dot|got_e, &yylval);
  1155.         if (toktype == ERROR)
  1156.       {
  1157.         char *err_copy = (char *) alloca (p - tokstart + 1);
  1158.  
  1159.         bcopy (tokstart, err_copy, p - tokstart);
  1160.         err_copy[p - tokstart] = 0;
  1161.         error ("Invalid number \"%s\".", err_copy);
  1162.       }
  1163.     lexptr = p;
  1164.     return toktype;
  1165.       }
  1166.  
  1167.     case '+':
  1168.     case '-':
  1169.     case '*':
  1170.     case '/':
  1171.     case '%':
  1172.     case '|':
  1173.     case '&':
  1174.     case '^':
  1175.     case '~':
  1176.     case '!':
  1177.     case '@':
  1178.     case '<':
  1179.     case '>':
  1180.     case '[':
  1181.     case ']':
  1182.     case '?':
  1183.     case ':':
  1184.     case '=':
  1185.     case '{':
  1186.     case '}':
  1187.     symbol:
  1188.       lexptr++;
  1189.       return c;
  1190.  
  1191.     case '"':
  1192.       for (namelen = 1; (c = tokstart[namelen]) != '"'; namelen++)
  1193.     if (c == '\\')
  1194.       {
  1195.         c = tokstart[++namelen];
  1196.         if (c >= '0' && c <= '9')
  1197.           {
  1198.         c = tokstart[++namelen];
  1199.         if (c >= '0' && c <= '9')
  1200.           c = tokstart[++namelen];
  1201.           }
  1202.       }
  1203.       yylval.sval.ptr = tokstart + 1;
  1204.       yylval.sval.length = namelen - 1;
  1205.       lexptr += namelen + 1;
  1206.       return STRING;
  1207.     }
  1208.  
  1209.   if (!(c == '_' || c == '$'
  1210.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  1211.     /* We must have come across a bad character (e.g. ';').  */
  1212.     error ("Invalid character '%c' in expression.", c);
  1213.  
  1214.   /* It's a name.  See how long it is.  */
  1215.   namelen = 0;
  1216.   for (c = tokstart[namelen];
  1217.        (c == '_' || c == '$' || (c >= '0' && c <= '9')
  1218.     || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  1219.        c = tokstart[++namelen])
  1220.     ;
  1221.  
  1222.   /* The token "if" terminates the expression and is NOT 
  1223.      removed from the input stream.  */
  1224.   if (namelen == 2 && tokstart[0] == 'i' && tokstart[1] == 'f')
  1225.     {
  1226.       return 0;
  1227.     }
  1228.  
  1229.   lexptr += namelen;
  1230.  
  1231.   /* Handle the tokens $digits; also $ (short for $0) and $$ (short for $$1)
  1232.      and $$digits (equivalent to $<-digits> if you could type that).
  1233.      Make token type LAST, and put the number (the digits) in yylval.  */
  1234.  
  1235.   if (*tokstart == '$')
  1236.     {
  1237.       register int negate = 0;
  1238.       c = 1;
  1239.       /* Double dollar means negate the number and add -1 as well.
  1240.      Thus $$ alone means -1.  */
  1241.       if (namelen >= 2 && tokstart[1] == '$')
  1242.     {
  1243.       negate = 1;
  1244.       c = 2;
  1245.     }
  1246.       if (c == namelen)
  1247.     {
  1248.       /* Just dollars (one or two) */
  1249.       yylval.lval = - negate;
  1250.       return LAST;
  1251.     }
  1252.       /* Is the rest of the token digits?  */
  1253.       for (; c < namelen; c++)
  1254.     if (!(tokstart[c] >= '0' && tokstart[c] <= '9'))
  1255.       break;
  1256.       if (c == namelen)
  1257.     {
  1258.       yylval.lval = atoi (tokstart + 1 + negate);
  1259.       if (negate)
  1260.         yylval.lval = - yylval.lval;
  1261.       return LAST;
  1262.     }
  1263.     }
  1264.  
  1265.   /* Handle tokens that refer to machine registers:
  1266.      $ followed by a register name.  */
  1267.  
  1268.   if (*tokstart == '$') {
  1269.     for (c = 0; c < NUM_REGS; c++)
  1270.       if (namelen - 1 == strlen (reg_names[c])
  1271.       && !strncmp (tokstart + 1, reg_names[c], namelen - 1))
  1272.     {
  1273.       yylval.lval = c;
  1274.       return REGNAME;
  1275.     }
  1276.     for (c = 0; c < num_std_regs; c++)
  1277.      if (namelen - 1 == strlen (std_regs[c].name)
  1278.      && !strncmp (tokstart + 1, std_regs[c].name, namelen - 1))
  1279.        {
  1280.      yylval.lval = std_regs[c].regnum;
  1281.      return REGNAME;
  1282.        }
  1283.   }
  1284.   /* Catch specific keywords.  Should be done with a data structure.  */
  1285.   switch (namelen)
  1286.     {
  1287.     case 8:
  1288.       if (!strncmp (tokstart, "unsigned", 8))
  1289.     return UNSIGNED;
  1290.       break;
  1291.     case 6:
  1292.       if (!strncmp (tokstart, "struct", 6))
  1293.     return STRUCT;
  1294.       if (!strncmp (tokstart, "signed", 6))
  1295.     return SIGNED;
  1296.       if (!strncmp (tokstart, "sizeof", 6))      
  1297.     return SIZEOF;
  1298.       break;
  1299.     case 5:
  1300.       if (!strncmp (tokstart, "union", 5))
  1301.     return UNION;
  1302.       if (!strncmp (tokstart, "short", 5))
  1303.     return SHORT;
  1304.       break;
  1305.     case 4:
  1306.       if (!strncmp (tokstart, "enum", 4))
  1307.     return ENUM;
  1308.       if (!strncmp (tokstart, "long", 4))
  1309.     return LONG;
  1310.       if (!strncmp (tokstart, "this", 4))
  1311.     {
  1312.       static const char this_name[] =
  1313.                  { CPLUS_MARKER, 't', 'h', 'i', 's', '\0' };
  1314.  
  1315.       if (lookup_symbol (this_name, expression_context_block,
  1316.                  VAR_NAMESPACE, 0, NULL))
  1317.         return THIS;
  1318.     }
  1319.       break;
  1320.     case 3:
  1321.       if (!strncmp (tokstart, "int", 3))
  1322.     return INT_KEYWORD;
  1323.       break;
  1324.     default:
  1325.       break;
  1326.     }
  1327.  
  1328.   yylval.sval.ptr = tokstart;
  1329.   yylval.sval.length = namelen;
  1330.  
  1331.   /* Any other names starting in $ are debugger internal variables.  */
  1332.  
  1333.   if (*tokstart == '$')
  1334.     {
  1335.       yylval.ivar =  lookup_internalvar (copy_name (yylval.sval) + 1);
  1336.       return VARIABLE;
  1337.     }
  1338.  
  1339.   /* Use token-type BLOCKNAME for symbols that happen to be defined as
  1340.      functions or symtabs.  If this is not so, then ...
  1341.      Use token-type TYPENAME for symbols that happen to be defined
  1342.      currently as names of types; NAME for other symbols.
  1343.      The caller is not constrained to care about the distinction.  */
  1344.   {
  1345.     char *tmp = copy_name (yylval.sval);
  1346.     struct symbol *sym;
  1347.     int is_a_field_of_this = 0;
  1348.     int hextype;
  1349.  
  1350.     sym = lookup_symbol (tmp, expression_context_block,
  1351.              VAR_NAMESPACE, &is_a_field_of_this, NULL);
  1352.     if ((sym && SYMBOL_CLASS (sym) == LOC_BLOCK) ||
  1353.         lookup_partial_symtab (tmp))
  1354.       {
  1355.     yylval.ssym.sym = sym;
  1356.     yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1357.     return BLOCKNAME;
  1358.       }
  1359.     if (sym && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  1360.         {
  1361.       yylval.tsym.type = SYMBOL_TYPE (sym);
  1362.       return TYPENAME;
  1363.         }
  1364.     if ((yylval.tsym.type = lookup_primitive_typename (tmp)) != 0)
  1365.     return TYPENAME;
  1366.  
  1367.     /* Input names that aren't symbols but ARE valid hex numbers,
  1368.        when the input radix permits them, can be names or numbers
  1369.        depending on the parse.  Note we support radixes > 16 here.  */
  1370.     if (!sym && 
  1371.         ((tokstart[0] >= 'a' && tokstart[0] < 'a' + input_radix - 10) ||
  1372.          (tokstart[0] >= 'A' && tokstart[0] < 'A' + input_radix - 10)))
  1373.       {
  1374.      YYSTYPE newlval;    /* Its value is ignored.  */
  1375.     hextype = parse_number (tokstart, namelen, 0, &newlval);
  1376.     if (hextype == INT)
  1377.       {
  1378.         yylval.ssym.sym = sym;
  1379.         yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1380.         return NAME_OR_INT;
  1381.       }
  1382.     if (hextype == UINT)
  1383.       {
  1384.         yylval.ssym.sym = sym;
  1385.         yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1386.         return NAME_OR_UINT;
  1387.       }
  1388.       }
  1389.  
  1390.     /* Any other kind of symbol */
  1391.     yylval.ssym.sym = sym;
  1392.     yylval.ssym.is_a_field_of_this = is_a_field_of_this;
  1393.     return NAME;
  1394.   }
  1395. }
  1396.  
  1397. void
  1398. yyerror (msg)
  1399.      char *msg;
  1400. {
  1401.   error ("Invalid syntax in expression.");
  1402. }
  1403.  
  1404. /* Table mapping opcodes into strings for printing operators
  1405.    and precedences of the operators.  */
  1406.  
  1407. const static struct op_print c_op_print_tab[] =
  1408.   {
  1409.     {",",  BINOP_COMMA, PREC_COMMA, 0},
  1410.     {"=",  BINOP_ASSIGN, PREC_ASSIGN, 1},
  1411.     {"||", BINOP_OR, PREC_OR, 0},
  1412.     {"&&", BINOP_AND, PREC_AND, 0},
  1413.     {"|",  BINOP_LOGIOR, PREC_LOGIOR, 0},
  1414.     {"&",  BINOP_LOGAND, PREC_LOGAND, 0},
  1415.     {"^",  BINOP_LOGXOR, PREC_LOGXOR, 0},
  1416.     {"==", BINOP_EQUAL, PREC_EQUAL, 0},
  1417.     {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
  1418.     {"<=", BINOP_LEQ, PREC_ORDER, 0},
  1419.     {">=", BINOP_GEQ, PREC_ORDER, 0},
  1420.     {">",  BINOP_GTR, PREC_ORDER, 0},
  1421.     {"<",  BINOP_LESS, PREC_ORDER, 0},
  1422.     {">>", BINOP_RSH, PREC_SHIFT, 0},
  1423.     {"<<", BINOP_LSH, PREC_SHIFT, 0},
  1424.     {"+",  BINOP_ADD, PREC_ADD, 0},
  1425.     {"-",  BINOP_SUB, PREC_ADD, 0},
  1426.     {"*",  BINOP_MUL, PREC_MUL, 0},
  1427.     {"/",  BINOP_DIV, PREC_MUL, 0},
  1428.     {"%",  BINOP_REM, PREC_MUL, 0},
  1429.     {"@",  BINOP_REPEAT, PREC_REPEAT, 0},
  1430.     {"-",  UNOP_NEG, PREC_PREFIX, 0},
  1431.     {"!",  UNOP_ZEROP, PREC_PREFIX, 0},
  1432.     {"~",  UNOP_LOGNOT, PREC_PREFIX, 0},
  1433.     {"*",  UNOP_IND, PREC_PREFIX, 0},
  1434.     {"&",  UNOP_ADDR, PREC_PREFIX, 0},
  1435.     {"sizeof ", UNOP_SIZEOF, PREC_PREFIX, 0},
  1436.     {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
  1437.     {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
  1438.     /* C++  */
  1439.     {"::", BINOP_SCOPE, PREC_PREFIX, 0},
  1440. };
  1441.  
  1442. /* These variables point to the objects
  1443.    representing the predefined C data types.  */
  1444.  
  1445. struct type *builtin_type_void;
  1446. struct type *builtin_type_char;
  1447. struct type *builtin_type_short;
  1448. struct type *builtin_type_int;
  1449. struct type *builtin_type_long;
  1450. struct type *builtin_type_long_long;
  1451. struct type *builtin_type_unsigned_char;
  1452. struct type *builtin_type_unsigned_short;
  1453. struct type *builtin_type_unsigned_int;
  1454. struct type *builtin_type_unsigned_long;
  1455. struct type *builtin_type_unsigned_long_long;
  1456. struct type *builtin_type_float;
  1457. struct type *builtin_type_double;
  1458.  
  1459. struct type ** const (c_builtin_types[]) = 
  1460. {
  1461.   &builtin_type_int,
  1462.   &builtin_type_long,
  1463.   &builtin_type_short,
  1464.   &builtin_type_char,
  1465.   &builtin_type_float,
  1466.   &builtin_type_double,
  1467.   &builtin_type_void,
  1468.   &builtin_type_long_long,
  1469.   &builtin_type_unsigned_char,
  1470.   &builtin_type_unsigned_short,
  1471.   &builtin_type_unsigned_int,
  1472.   &builtin_type_unsigned_long,
  1473.   &builtin_type_unsigned_long_long,
  1474.   0
  1475. };
  1476.  
  1477. /* FIXME:  Eventually do a separate defintion for C++.  */
  1478.  
  1479. const struct language_defn c_language_defn = {
  1480.   "c",                /* Language name */
  1481.   language_c,
  1482.   c_builtin_types,
  1483.   range_check_off,
  1484.   type_check_off,
  1485.   c_parse,
  1486.   c_error,
  1487.   &BUILTIN_TYPE_LONGEST,     /* longest signed   integral type */
  1488.   &BUILTIN_TYPE_UNSIGNED_LONGEST,/* longest unsigned integral type */
  1489.   &builtin_type_double,        /* longest floating point type */
  1490.   "0x%x", "0x%", "x",        /* Hex   format, prefix, suffix */
  1491.   "0%o",  "0%",  "o",        /* Octal format, prefix, suffix */
  1492.   c_op_print_tab,        /* expression operators for printing */
  1493.   LANG_MAGIC
  1494. };
  1495.  
  1496. void
  1497. _initialize_c_exp ()
  1498. {
  1499.   /* FIXME:  The code below assumes that the sizes of the basic data
  1500.      types are the same on the host and target machines!!!  */
  1501.  
  1502.   builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void");
  1503.  
  1504.   builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
  1505.   builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
  1506.  
  1507.   builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
  1508.   builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
  1509.   builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
  1510.   builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
  1511.  
  1512.   builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
  1513.   builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
  1514.   builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
  1515.   builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
  1516.  
  1517.   builtin_type_long_long =
  1518.     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
  1519.            0, "long long");
  1520.   builtin_type_unsigned_long_long = 
  1521.     init_type (TYPE_CODE_INT, TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
  1522.            1, "unsigned long long");
  1523.  
  1524.   add_language (&c_language_defn);
  1525.   set_language (language_c);        /* Make C the default language */
  1526. }
  1527.